﻿// Decompiled with JetBrains decompiler
// Type: TextConverter.Converter
// Assembly: TextConverter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 81592A1B-CF4D-47A7-854E-6F09F1A28FCB
// Assembly location: D:\Work\ObjEditoe_Update\ObjectEditor\Project\Assets\Plugins\StickerSystem\TextConverter.dll

using System.Collections;
using UnityEngine;
using UnityEngine.UI;

namespace TextConverter
{
    public class Converter : MonoBehaviour
    {
        /// <summary>
        /// 文字转贴图
        /// </summary>
        /// <param name="text"></param>
        /// <param name="targetFont"></param>
        /// <param name="handleScale"></param>
        /// <param name="isVirtical"></param>
        /// <param name="fontSize">字号</param>
        /// <returns></returns>
        public Texture GenerateTexture(string text, Font targetFont, int fontSize, Vector2 handleScale,
            bool isVirtical)
        {
            float aspect = handleScale.x / handleScale.y;
            Canvas tempCanvas = new GameObject("TempCanvas").AddComponent<Canvas>();
            tempCanvas.renderMode = RenderMode.ScreenSpaceCamera;

            GameObject gameObject1 = new GameObject("StickerText");
            Text textMesh = null;
            if (isVirtical)
            {
                textMesh = gameObject1.AddComponent<TextVirtical>();
            }
            else
            {
                textMesh = gameObject1.AddComponent<Text>();
            }

            RectTransform textRect = gameObject1.GetComponent<RectTransform>();
            gameObject1.transform.SetParent(tempCanvas.transform, true);

            textRect.anchoredPosition = Vector2.zero;

            Texture mainTexture = targetFont.material.mainTexture;
            textMesh.alignment = TextAnchor.MiddleCenter;
            textMesh.font = targetFont;

            Material material = new Material(Shader.Find("Custom/3D Text Shader"));
            material.SetTexture("_MainTex", mainTexture);
            textMesh.material = material;
            textMesh.text = text;
            string[] lineStr = text.Split('\n');
            int strMaxLength = -1;
            string str = string.Empty;
            for (int i = 0; i < lineStr.Length; i++)
            {
                if (str.Length < lineStr[i].Length)
                {
                    str = lineStr[i];
                }
            }

            strMaxLength = str.Length;

            if (!isVirtical)
            {
                textRect.sizeDelta = new Vector2(handleScale.x / handleScale.y * 300, 300);
            }
            else
            {
                textRect.sizeDelta = new Vector2(300, (fontSize * strMaxLength) + (strMaxLength * 10));
            }

            if (fontSize < 20)
            {
                fontSize = 20;
            }

            textMesh.fontSize = fontSize;

            GameObject gameObject2 = new GameObject("RenderCam");
            Camera camera = gameObject2.AddComponent<Camera>();
            camera.clearFlags = CameraClearFlags.Color;
            camera.backgroundColor = new Color(0.0f, 0.0f, 0.0f, 0.0f);
            gameObject2.transform.position = gameObject1.transform.position - gameObject1.transform.forward * 10f;
            gameObject2.transform.SetParent(gameObject1.transform, true);
            camera.orthographic = true;
            camera.aspect = aspect;
            camera.orthographicSize = 1000;
            tempCanvas.worldCamera = camera;

            RenderTexture renderTexture = new RenderTexture(Mathf.CeilToInt(textRect.sizeDelta.x), Mathf.CeilToInt(textRect.sizeDelta.y), 0);
            camera.targetTexture = renderTexture;
            camera.Render();

            this.StartCoroutine(this.DestorTempObj(camera, gameObject1, tempCanvas.gameObject));

            return renderTexture;
        }

        private IEnumerator DestorTempObj(Camera camera, GameObject gameObject1, GameObject tempCanvas)
        {
            yield return new WaitForEndOfFrame();
            camera.targetTexture = (RenderTexture)null;
            GameObject.DestroyImmediate(gameObject1);
            GameObject.DestroyImmediate(tempCanvas);
        }
    }
}
